import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { ApiService } from './api.service';

@Injectable({
  providedIn: 'root'
})
export class MainService {

  headers = new Headers({'Content-Type':'application/json'})
  mainUrl = this.apiService.apiUrl
  picurl = this.apiService.picurl
  constructor(private http:Http,private apiService:ApiService) { 
    
   }
  getFromServ(url,params,successCallBack,errorCallBack){
    this.http.get(this.mainUrl+url,params).subscribe(
      (res)=>{        
        // if(JSON.parse((<any>res)._body).code==200){
          successCallBack(JSON.parse((<any>res)._body))
        //  }else{
        //   successCallBack(JSON.parse((<any>res)._body));
        //  }
        
      },(error)=>{
        errorCallBack(error);
        //console.log(error);
      }
    )
  }
  postMsgToServer(url,params,successCallBack,errorCallBack){
    // //console.log(url);
    // console.log(params);
    
    // const mainUrl = 'http://47.105.113.108/xianyu/public'
    
      this.http.post(this.mainUrl+url,params,{headers:this.headers}).subscribe(
        (res)=>{        
          if(JSON.parse((<any>res)._body).code==200){
            successCallBack(JSON.parse((<any>res)._body))
           }else{
            successCallBack(JSON.parse((<any>res)._body));
           }
          
        },(error)=>{
          errorCallBack(error);
          //console.log(error);
        }
      )
  }
  setLocalStorage(key,value){
    let a  = JSON.stringify(value)
     window.localStorage.setItem(key,a);
  
  }
  getLocalStorage(key){
    let a ;
    a  = window.localStorage.getItem(key);
    return JSON.parse(a);
  }
  deleteLocalStorage(key){
    window.localStorage.removeItem(key)
  }
  getFirstFLList(id,data){
    let a 
    this.getFromServ('/api/index/get_list'+'?id='+id,{},res=>{
     data(res);
      
    },err=>{})
    
  }
  
  
}
